using System;
using System.Collections.Generic;
using System.Text;
using RandomGenerationFramework;
using System.Collections;
using System.Diagnostics;

namespace DBRandomGenerator
{
    public static class DataBuilder
    {

        public static string _binary = "binary";
        public static string _bigint = "bigint";
        public static string _bit = "bit";
        public static string _char = "char";
        public static string _datetime = "datetime";
        public static string _decimal = "decimal";
        public static string _float = "float";
        public static string _image = "image";
        public static string _int = "int";
        public static string _money = "money";
        public static string _nchar = "nchar";
        public static string _ntext = "ntext";
        public static string _nvarchar = "nvarchar";
        public static string _numeric = "numeric";
        public static string _real = "real";
        public static string _smalldatetime = "smalldatetime";
        public static string _smallint = "smallint";
        public static string _smallmoney = "smallmoney";
        public static string _sql_variant = "sql_variant";
        public static string _sysname = "sysname";
        public static string _text = "text";
        public static string _timestamp = "timestamp";
        public static string _tinyint = "tinyint";
        public static string _varbinary = "varbinary";
        public static string _varchar = "varchar";
        public static string _uniqueidentifier = "uniqueidentifier";


        public static string GetBinary(Column TableColumn)
        {
            try
            {
                lock (_binary)
                {
                    // generate a random length Binary 
                    ByteRandomGen list = new ByteRandomGen(TableColumn.Length);
                    string ColumnValue = "CAST( '";
                    ColumnValue = ColumnValue + list.GetRandom().ToString();
                    ColumnValue = ColumnValue + "'";
                    ColumnValue = ColumnValue + "   AS BINARY)";
                    return ColumnValue;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public static string GetBit()
        {
            try
            {
                lock (_binary)
                {
                    BooleanRandonGen bg = new BooleanRandonGen();
                    return Convert.ToInt32(bg.GetRandom()).ToString();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static string GetBigInt(Column TableColumn)
        {
            try
            {
                lock (_binary)
                {
                    string ColumnValue = String.Empty;
                    Random Length = new Random();
                    IntegerRandomGenerator ig = new IntegerRandomGenerator(Length.Next(1, System.Int64.MaxValue.ToString().Length), Length.Next(1, System.Int64.MaxValue.ToString().Length));
                    int DataLength = Length.Next(1, System.Int64.MaxValue.ToString().Length);
                    for (int i = 0; i < DataLength.ToString().Length; i++)
                        ColumnValue = ColumnValue + ig.GetRandom().ToString();
                    return ColumnValue;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static string GetChar(Column TableColumn)
        {
            try
            {
                lock (_binary)
                {
                    string ColumnValue = String.Empty;
                    CharRandomGen chargen = new CharRandomGen();
                    ColumnValue = ColumnValue + chargen.GetRandom().ToString();
                    return ColumnValue;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static string GetString(Column TableColumn)
        {
            try
            {
                lock (_binary)
                {

                    string ColumnValue = String.Empty;
                    StringRandomGenerator sg1 = new StringRandomGenerator(10, CharacterType.LowerCase);
                    ColumnValue = sg1.GetRandom();
                    return ColumnValue;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static string GetDateTime()
        {
            try
            {
                lock (_binary)
                {
                    string ColumnValue = String.Empty;
                    DateRandomGenerator tg = new DateRandomGenerator(new DateTime(1969, 1, 1), new DateTime(2009, 2, 28));
                    ColumnValue = tg.GetRandom().ToString("MM/dd/yyyy");
                    return ColumnValue;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static string GetInt(Column TableColumn)
        {
            try
            {
                lock (_binary)
                {
                    IntegerRandomGenerator ig = new IntegerRandomGenerator(1, int.MaxValue.ToString().Length);
                    return ig.GetRandom().ToString();
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public static string GetUniqueIdentifier(Column TableColumn)
        {
            try
            {
                lock (_binary)
                {
                    GuidRandomGen ig = new GuidRandomGen();
                    return ig.GetRandom().ToString();
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public static string GetSqlVariant(Column TableColumn)
        {
            try
            {
                lock (_binary)
                {
                    GuidRandomGen ig = new GuidRandomGen();
                    Guid temp = ig.GetRandom();
                    if (temp.ToString().Length > TableColumn.Length)
                    {
                        return ig.GetRandom().ToString();
                    }
                    return ig.GetRandom().ToString().Substring(0, TableColumn.Length);
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public static string GetID(Column TableColumn)
        {
            try
            {
                lock (_binary)
                {
                    //string ColumnValue = "";
                    Random Length = new Random();
                    int DataLength = RandomGeneratorHelper.GetID(1, System.Int64.MaxValue.ToString().Length);
                    return DataLength.ToString();
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public static string GetTinyInt()
        {
            try
            {
                IntegerRandomGenerator ig = new IntegerRandomGenerator(1, 255);
                return ig.GetRandom().ToString();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public static string GetSmallInt(Column TableColumn)
        {
            try
            {
                lock (_binary)
                {
                    string ColumnValue = String.Empty;
                    IntegerRandomGenerator ig = new IntegerRandomGenerator(1, System.Int64.MaxValue.ToString().Length);
                    int DataLength = ig.GetRandom();
                    for (int i = 1; i <= DataLength; i++)
                    {
                        ColumnValue = ColumnValue + ig.GetRandom().ToString();
                    }
                    return ColumnValue;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        public static string GetText(Column TableColumn)
        {
            try
            {
                lock (_binary)
                {
                    string ColumnValue = String.Empty;
                    // decide average number of strings to generate in this case
                    int numStrings = (TableColumn.Length / 4);
                    ArrayList a = RandomGeneratorHelper.GetRandomInteger(1, 10, numStrings);
                  //  Debug.WriteLine("Returned from GET TEXT service call at  " + DateTime.Now.ToString("HH:mm:ss"));
                  //  int b = 0;
                     StringRandomGenerator sg1 = new StringRandomGenerator(10, CharacterType.LowerCase);
                    foreach(int c in a) 
                    {
                        ArrayList b = RandomGeneratorHelper.GetRandomInteger(0, 25, c);
                        ColumnValue += " " + sg1.GetRandom(c, b);
                        if(ColumnValue.Length >TableColumn.Length)
                            break;
                        b = null;
                    }
                    Debug.WriteLine(" Completed Text Generation at  " + DateTime.Now.ToString("HH:mm:ss"));
                    return ColumnValue;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
